- Update .csdef file with new variable for Service Bus Connection by adding the following to both the Webrole and worker roles at end of each section:

<ConfigurationSettings>
      <Setting name="Microsoft.ServiceBus.ConnectionString" />
</ConfigurationSettings>

- Edit Cloud.cscfg file with new connection string and update BOTH web and worker roles sections:

    <ConfigurationSettings>
      <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
      <Setting name="Microsoft.ServiceBus.ConnectionString" value="<Your ACS Connection String>" />
    </ConfigurationSettings>

-Edit Local.cscfg with new connection string for both web and worker roles as follows (we will point it to local storage)

    <ConfigurationSettings>
      <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
      <Setting name="Microsoft.ServiceBus.ConnectionString" value="<Your ACS Connection String>" />
    </ConfigurationSettings>

- Open /Controllers/HomeController.cs and add Azure namespace reference (if you have errors you forgot to add ref to Microsoft.ServiceBus dll v 2.0.0 under project references):

using Microsoft.WindowsAzure;
using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;

- Add System.Runtime.Serialization dll as a reference to project

- Add the following as class vars:

string connectionString =  CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
 string qname = "signups";

- Add following code to Newsletter method at very beginning (you may need to also add Microsoft.ServiceBus ref.  You can skip this step if you're not following along in the video and just want to reproduce the code.)

var nm = NamespaceManager.CreateFromConnectionString(connectionString);
            if (!nm.QueueExists(qname))
            {
                nm.CreateQueue(qname);
            }

- Delete queue from portal and replace code at top of Newsletter with the following:

    var nm = NamespaceManager.CreateFromConnectionString(connectionString);
            QueueDescription qd = new QueueDescription(qname);
            qd.MaxSizeInMegabytes = 2048;  //Max size of queue is 2GB
            qd.DefaultMessageTimeToLive = new TimeSpan(0, 5, 0); //Max TTL is 5 minutes

            if (!nm.QueueExists(qname))
            {
                nm.CreateQueue(qd);
            }

- Now we need to add a msg to the queue.  Insert the following code after the CreateQueue code you inserted from the previous step.

            //Send to the Queue
            QueueClient qc = QueueClient.CreateFromConnectionString(connectionString, qname);

            //Create msg with email property and send to QueueClient
            var bm = new BrokeredMessage();
            bm.Properties["email"] = email;
            qc.Send(bm);

- Now we need to update the Worker Role. First lets update namespace refs by adding the following and ref the same Service Bus dll v 2.0.0 in projects refs.

using Microsoft.ServiceBus;
using Microsoft.ServiceBus.Messaging;

- Add the following as class vars at WorkerRole:RoleEntryPoint :

   string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
string qname = "signups";

- Erase the existing Thread.sleep in Run method 
	Thread.Sleep(10000);
	Trace.WriteLine("Working", "Information");

- Replace with the following:

QueueClient qc = QueueClient.CreateFromConnectionString(connectionString, qname);
                BrokeredMessage msg = qc.Receive();

                if (msg != null)
                {
                    try
                    {
                        Trace.WriteLine("New Signup processed: " + msg.Properties["email"]);
                        msg.Complete();

                    }
                    catch (Exception)
                    {
                        // Indicate a problem, unlock message in queue
                        msg.Abandon();
                    }
                }
- Add new class var where qname is:



       string tableConnectionString = CloudConfigurationManager.GetSetting("TableStorageConnection");

- Add the following new method call after msg.Complete():


                        //Log to Table Storage
                        SaveToStorage(msg.Properties["email"].ToString());

- Create new class called Person.cs that iherits from TableServiceEntity.  Do this by adding ref for using Microsoft.WindowsAzure.StorageClient (v 1.7) and following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.StorageClient;

namespace SignupsWorker
{
    class PersonEntity : TableServiceEntity
    {
        public PersonEntity() { }

        public string Email { get; set; }
    }

}

- Open back up WorkerRole.cs and add following ref to top (comment out using Microsoft.WindowsAzure.Storage; if it exists)

using Microsoft.WindowsAzure.StorageClient;

- Implement SaveToStorage as follows:

 private void SaveToStorage(string email)
        {
            string tableName = "signups";
            //Connection to Table Storage
            CloudStorageAccount account = CloudStorageAccount.Parse(tableConnectionString);

            //Client for Table Storage
            CloudTableClient tableStorage = account.CreateCloudTableClient();

            tableStorage.CreateTableIfNotExist(tableName);

            //Create new perosn object and set email
            TableServiceContext context = tableStorage.GetDataServiceContext();
            PersonEntity person = new PersonEntity();
            person.Email = email;
            person.PartitionKey = "signups";
            person.RowKey = email;

            //Save new person out to signups table
            context.AddObject(tableName, person);
            context.SaveChanges();


        }

